package cs1.game.main;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import cs.game.util.Bullet;
import cs.game.util.Enemy;
import cs.game.util.Flyer;
import cs.game.util.Mine;
import cs.game.util.Reward;

public class Game extends JPanel implements KeyListener,ActionListener {
	public static BufferedImage bgImg;
	public static Integer bgWidth;
	public static Integer bgHeigh;
	public static BufferedImage mineImg;
	public static Mine mine;
	public static final Integer toolHeidgh=30;
	public static ArrayList<Bullet> bullets;
	public static BufferedImage bulletImg;
	public static ArrayList<Enemy> enemies;
	public static BufferedImage enemyImg;
	public static ArrayList<Reward> reward;
	public static BufferedImage xingImg;
	public Timer timer = new Timer(100,this);
	
	static {
		try{
			bgImg = ImageIO.read(new File("image/bg.jpg"));
			bgWidth = bgImg.getWidth();
			bgHeigh = bgImg.getHeight();
			mineImg =ImageIO.read(new File("image/mine.png"));
			mine = new Mine(mineImg,(bgWidth-mineImg.getWidth())/2,bgHeigh-mineImg.getHeight());
			bullets = new ArrayList<Bullet>();
			enemies = new ArrayList<Enemy>();
			reward = new ArrayList<Reward>();
			bulletImg = ImageIO.read(new File("image/zd.png"));
			enemyImg = ImageIO.read(new File("image/menmy.png"));
			xingImg = ImageIO.read(new File("image/xing.png"));
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	protected void paintComponent(Graphics g) {
		g.drawImage(bgImg, 0, 0, null);
		g.drawImage(mine.image, mine.x, mine.y, null);
		mine.stepMove();
		mine.OutOfBounds();
		
		ArrayList<Flyer> outFlyer = new ArrayList<Flyer>();
			for(Bullet bullet:bullets){
				g.drawImage(bullet.image, bullet.x, bullet.y, null);
				bullet.stepMove();
				if(bullet.OutOfBounds()){
					outFlyer.add(bullet);
				}
			}
			bullets.removeAll(outFlyer);
			outFlyer.clear();
			for(Enemy e :enemies){
				g.drawImage(e.image, e.x, e.y, null);
				e.stepMove();
				if(e.OutOfBounds()){
					outFlyer.add(e);
				}
			}
			enemies.removeAll(outFlyer);
			ArrayList<Flyer> hitzd = new ArrayList<Flyer>();
			ArrayList<Flyer> hitenemies = new ArrayList<Flyer>();
			for(Enemy e:enemies){
				for(Bullet bullet:bullets){
					if(e.beShoot(bullet)){
						hitzd.add(bullet);
						hitenemies.add(e);
					}
				}
				if(e.beShoot(mine)){
					hitenemies.add(e);
				}
			}
			bullets.removeAll(hitzd);
			enemies.removeAll(hitenemies);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(new Random().nextInt(5)<3){
			mine.shoot();
		}
		createEnemy();
		repaint();
		createReward();
	}
	@Override
	public void keyPressed(KeyEvent e) {
		mine.direction=e.getKeyChar();
	}
	@Override
	public void keyReleased(KeyEvent e) {
		mine.direction= ' ';
	}
	@Override
	public void keyTyped(KeyEvent e) {
	}
	public void start() {
		this.addKeyListener(this);
		this.setFocusable(true);
		timer.start();
	}
	private void createEnemy() {
		if(new Random().nextInt(5)<3){
			Enemy enemy = new Enemy(enemyImg, new Random().nextInt(bgWidth-enemyImg.getWidth()), -enemyImg.getHeight());
			enemies.add(enemy);
		}
	}
	private void createReward() {
		if(new Random().nextInt(5)<3){
			Reward reward = new Reward(xingImg, new Random().nextInt(bgWidth-xingImg.getWidth()), -xingImg.getHeight());
			
		}
	}
}
